package com.tinyshardingjdbc.tinyshardingjdbc.demo.model.vetcvehiclebill.service;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tinyshardingjdbc.tinyshardingjdbc.demo.model.tool.PageResult;
import com.tinyshardingjdbc.tinyshardingjdbc.demo.model.tool.SmartPageUtil;
import com.tinyshardingjdbc.tinyshardingjdbc.demo.model.vetcvehiclebill.dao.VetcVehicleBillDao;
import com.tinyshardingjdbc.tinyshardingjdbc.demo.model.vetcvehiclebill.domain.form.VetcVehicleBillQueryForm;
import com.tinyshardingjdbc.tinyshardingjdbc.demo.model.vetcvehiclebill.domain.vo.VetcVehicleBillVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class VetcVehicleBillService {

    @Autowired
    private VetcVehicleBillDao vetcVehicleBillDao;


    /**
     * 分页查询
     *
     * @param queryForm
     * @return
     */
    public PageResult<VetcVehicleBillVO> queryPage(VetcVehicleBillQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<VetcVehicleBillVO> list = vetcVehicleBillDao.queryPage(page, queryForm);
        PageResult<VetcVehicleBillVO> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;
    }

    public Object test() {
        try {
            vetcVehicleBillDao.selectById(20230301);
        } catch (Exception E) {
            E.printStackTrace();
        }

        return 1;
    }


    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    public Object batchDelete(List<String> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return "";
        }

        vetcVehicleBillDao.deleteBatchIds(idList);
        return "";
    }

    /**
     * 单个删除
     */
    public Object delete(String billId) {
        if (null == billId) {
            return "";
        }

        vetcVehicleBillDao.deleteById(billId);
        return "";
    }

    /**
     * 单个删除
     */
    public Object deletes() {

        String a = "2024-01-04";
        String b = "20240101";
//        VetcVehicleBillEntity vetcVehicleBillEntity=new VetcVehicleBillEntity();
//        vetcVehicleBillEntity.setBillDate("2024-01-02");
//        vetcVehicleBillEntity.setBillId("20240102009890");
//        vetcVehicleBillEntity.setVehiclePlate("京A123456");
//        vetcVehicleBillDao.insert(vetcVehicleBillEntity);

        vetcVehicleBillDao.selectById("20230401");
        return "";
    }

    /**
     * 根据账单获取账单
     */
    public List<VetcVehicleBillVO> selebyId(List<String> ids) {
        Map<String, List<String>> map = listMap(ids);
        List<VetcVehicleBillVO> list = new ArrayList<>();
        for (Map.Entry<String, List<String>> entry : map.entrySet()) {

            try {
                List<VetcVehicleBillVO> list1 = vetcVehicleBillDao.selectByIds(entry.getValue());
                if (list1 != null && list1.size() > 0) {
                    list.addAll(list1);

                }
            } catch (Exception e) {

            }
        }

        return list;

    }

    public Map<String, List<String>> listMap(List<String> ids) {
        Map<String, List<String>> map = new HashMap<>();
        for (String str : ids) {
            String key = str.substring(0, 6);
            if (map.containsKey(key)) {
                map.get(key).add(str);
            } else {
                List<String> list = new ArrayList<>();
                list.add(str);
                map.put(key, list);
            }
        }

        return map;
    }


}
